package com.joy.joyusers.config;

import lombok.Data;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * Redisson 配置
 */
@Data
@Configuration
@ConfigurationProperties(prefix = "spring.redis")
public class RedissonConfig {

    private String host;

    private String port;

    /**
     * 创建Redisson客户端
     * @return
     */
    @Bean
    public RedissonClient redissonClient() {

        // 1. 创建配置对象
        Config config = new Config();
        String redisAddress = String.format("redis://%s:%s", host, port);
        config.useSingleServer().setAddress(redisAddress).setDatabase(0);   // 用3隔开
        // useClusterServers()是使用集群，这里没有就使用单个
        // use "rediss://" for SSL connection
        //.addNodeAddress("redis://127.0.0.1:7181");

        // 也可以读取配置文件的形式
        // config = Config.fromYAML(new File("config-file.yaml"));

        // 2. 创建redisson实例

        // 异步和事件驱动，Reactive API：RedissonReactiveClient redissonReactive = redisson.reactive();
        // RxJava3写法 APIs：RedissonRxClient redissonRx = redisson.rxJava();
        // 异步：Sync and Async API
        RedissonClient redisson = Redisson.create(config);




        return redisson;


    }

}
